package com.youku.core.model
{
   import flash.events.EventDispatcher;
   import flash.net.URLLoader;
   import flash.utils.Timer;
   import com.youku.interfaces.IPlayListData;
   import com.youku.GlobalService;
   import com.youku.GlobalServiceKeys;
   import com.youku.P2PConfig;
   import flash.events.Event;
   import com.adobe.json.JSON;
   import com.youku.core.view.events.PlayPolicyEvent;
   import flash.events.TimerEvent;
   import com.youku.PlayerConfig;
   import flash.net.URLRequest;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   
   public class PlayPolicyProxy extends EventDispatcher
   {
      
      public function PlayPolicyProxy() {
         super();
         this._timer = new Timer(this._loadTimeout);
         this._timer.addEventListener(TimerEvent.TIMER,this.onTimeout);
         this._loader = new URLLoader();
         this._loader.addEventListener(Event.COMPLETE,this.onLoadComplete);
         this._loader.addEventListener(IOErrorEvent.IO_ERROR,this.onLoadError);
         this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onLoadError);
      }
      
      private var _loader:URLLoader;
      
      private var _timer:Timer;
      
      private var _loadTimeout:Number = 3000.0;
      
      private const PLAY_POLICY_URL:String = "http://yk.pp.navi.youku.com/playpolicy/get.json";
      
      private function get playlistData() : IPlayListData {
         return GlobalService.getService(GlobalServiceKeys.PLAYLIST_DATA) as IPlayListData;
      }
      
      private function isAllow(param1:String) : Boolean {
         switch(param1)
         {
            case "player_ss":
               return P2PConfig.InitP2PType == "ss";
            case "player_pw":
               return P2PConfig.InitP2PType == "ppweb";
            case "player_yk":
               return P2PConfig.InitP2PType == "youku-p2p";
            default:
               return false;
         }
      }
      
      private function onLoadComplete(param1:Event) : void {
         this._timer.stop();
         var _loc2_:* = false;
         var _loc3_:String = URLLoader(param1.currentTarget).data;
         var _loc4_:Object = {};
         try
         {
            _loc4_ = com.adobe.json.JSON.decode(_loc3_);
         }
         catch(e:Error)
         {
         }
         if(_loc4_.hasOwnProperty("dataset"))
         {
            if(this.isAllow(_loc4_.dataset.p2p[0].player))
            {
               _loc2_ = true;
            }
            if(P2PConfig.isYoukuP2PPlayer)
            {
               P2PConfig.isYKUseP2P = _loc2_;
            }
         }
         dispatchEvent(new PlayPolicyEvent(PlayPolicyEvent.PLAY_POLICY_READY,{"allow":_loc2_}));
      }
      
      private function onLoadError(param1:Event) : void {
         this._timer.stop();
         try
         {
            this._loader.close();
         }
         catch(e:Error)
         {
         }
         dispatchEvent(new PlayPolicyEvent(PlayPolicyEvent.PLAY_POLICY_READY,{"allow":false}));
      }
      
      private function onTimeout(param1:TimerEvent) : void {
         this.onLoadError(null);
      }
      
      public function loadPolicy() : void {
         var _loc1_:* = "";
         _loc1_ = this.PLAY_POLICY_URL;
         _loc1_ = _loc1_ + "?";
         _loc1_ = _loc1_ + ("vid_encode=" + this.playlistData.vidEncoded);
         _loc1_ = _loc1_ + ("&category=" + this.playlistData.categories);
         _loc1_ = _loc1_ + ("&dma_code=" + this.playlistData.controller.dma_code);
         _loc1_ = _loc1_ + ("&area_code=" + this.playlistData.controller.area_code);
         _loc1_ = _loc1_ + ("&p2p_partner=" + P2PConfig.InitP2PType);
         if(PlayerConfig.isTudouPlayer)
         {
            _loc1_ = _loc1_ + ("&site=" + "tudou");
         }
         var _loc2_:URLRequest = new URLRequest(_loc1_);
         this._loader.load(_loc2_);
         this._timer.reset();
         this._timer.start();
      }
   }
}
